package com.xmg.sort;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

public class MergeDemo {

    public static void main(String[] args) {
        int[] arr = {4,3,7,5,8,9,2,1,5,6};
        int[] temp = new int[arr.length];
        mergeSort(arr,0,arr.length - 1,temp);
        System.out.println(Arrays.toString(arr));
        ClassLoader loader = MergeDemo.class.getClassLoader();
        System.out.println(loader);
        System.out.println(String.class.getClassLoader());
        ThreadLocal<Integer> local = new ThreadLocal<>();
        local.set(1);

        Set<Integer> set = new HashSet<>();
        set.add(null);
        set.add(1);
        System.out.println(set.contains(null));
    }

    public static void mergeSort(int[] arr,int left,int right,int[] temp){
        if (left<right){
            int mid = (left + right) / 2;
            mergeSort(arr,left,mid,temp);

            mergeSort(arr,mid+1,right,temp);

            merge(arr,left,mid,right,temp);
        }
    }

    public static void merge(int[] arr,int left,int mid,int right,int[] temp){
        int i = left;
        int j = mid+1;
        int t = 0;
        while (i<=mid && j<= right){
            if(arr[i]<=arr[j]){
                temp[t] = arr[i];
                i++;
                t++;
            }else{
                temp[t] = arr[j];
                j++;
                t++;
            }
        }
        while (i<=mid){
            temp[t] = arr[i];
            i++;
            t++;
        }
        while (j<=right){
            temp[t] = arr[j];
            j++;
            t++;
        }

        t=0;
        int templeft = left;
        while (templeft<=right){
            arr[templeft] = temp[t];
            templeft++;
            t++;
        }
    }
}
